// pages/apply_refund/index.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isTanShow:false,
        totalMoney:0,
        mothod:[
          {
            id:0,
            title:'仅退款'
          },
          {
            id: 1,
            title: '退货退款'
          }
        ],
        reason:'请选择',
        orderid:0,
        goods: [],
        reasonList:[],
        refund_instruction:'',
        title:'仅退款',
        methodid:0,
    },
  bindPickerChange: function (e) {
    //console.log(e.detail.value)
    var that = this;
    var mothod = that.data.mothod;
    var title = mothod[e.detail.value].title;
    var id = mothod[e.detail.value].id;
    //console.log(id)
    that.setData({
      title: title,
      methodid: id,
    })
  },
  getRefundInstruction:function(e){
    this.setData({
      refund_instruction:e.detail.value,
    })
  },
    closeShow:function(){
        var that=this;
        that.setData({
            isTanShow:false,
        })
    },
    cancel:function(){
      var that=this;
      var reasonList=that.data.reasonList;
      reasonList.map((elem)=>{
        elem.isChoose=false;
      })
      that.setData({
        reasonList: reasonList,
        reason:'请选择',
      })
      that.closeShow();
    },
    open:function(){
        var that = this;
        that.setData({
            isTanShow: true,
        })
    },
    choose:function(e){
        var index=e.currentTarget.id;
        var that=this;
        var reason = that.data.reason;
        var reasonList = that.data.reasonList;
        reasonList.forEach(item=>{
            item.isChoose=false;
        })
        reason=reasonList[index].value;
        reasonList[index].isChoose=true;
        that.setData({
            reason:reason,
            reasonList: reasonList,
        })
      that.closeShow();
    },
    sure:function(){
        var that=this;
        if(that.data.reason==='请选择'){
            wx.showToast({
                title: '请选择退款原因',
                image:'/images/fail.png',
                duration:1500
            })
        }else{
          var orderid = that.data.orderid;
          var openid = wx.getStorageSync('openid');
          var refoundtype = that.data.methodid;
          var reason = that.data.reason;
          var refoundbz = that.data.refund_instruction;

          wx.request({
            url: Host + 'Mine/savebackApply',
            data: { orderid, openid, refoundtype, reason, refoundbz },
            method: 'post',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              //console.log(res.data);
              if (res.data.code == 200) {
                wx.showToast({
                  title: '提交成功,等待商家同意',
                  icon: 'none',
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/refund/refund',
                  }) 
                }, 1000)
              }else {
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'none',
                })
              }
            }
          })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      that.setData({
        orderid: options.id
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var that = this;
      var orderid = that.data.orderid;
      wx.request({
        url: Host + 'Mine/drawbackApply',
        data: { orderid},
        success: function (res) {
          //console.log(res.data);
          if (res.data.code == 200) {
            that.setData({
              goods: res.data.ordergoods,
              totalMoney: res.data.order.trueprice,
              reasonList: res.data.reasonList
            })
          }
        }
      })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})